package com.yazi.mxz.behavior.controller;

import javax.annotation.Resource;

import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.behavior.common.BaseController;
import com.yazi.mxz.behavior.entity.repository.Region;
import com.yazi.mxz.behavior.jackson.filter.RegionFilter;
import com.yazi.mxz.behavior.repository.RegionRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("regionController")
@RequestMapping("/region")
public class RegionController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(RegionController.class);

	@Resource
	RegionRepository regionRepository;

	/**
	 * 查询离(lng,lat)最近的地区
	 * 
	 * @param x
	 *            lng
	 * @param y
	 *            lat
	 * @return
	 */
	@RequestMapping("/near/json")
	@ResponseBody
	public ObjectResult<?> nearJson(Double x, Double y) {
		
		ObjectResult<Region> jsonResult = new ObjectResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		// 默认在南宁地区
		Point point = new Point(108.3, 22.8);
		try {

			if (null != x && null != y) {
				point = new Point(x, y);
			}
			
			log.debug("nearJson: {},{}", x, y);

			jsonResult.setResult(regionRepository.findTop1ByLocationNear(point));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/listByParentId/json")
	@ResponseBody
	public ListResult<?> listByParentIdJson(long parentId) {
		log.debug("获取子级地区: parentId={}", parentId);
		ListResult<Region> jsonResult = new ListResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			jsonResult.setResult(regionRepository.findByParentIdOrderByRegionId(parentId));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取子级地区列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/search/json")
	@ResponseBody
	public ObjectResult<?> searchJson(String searchWord) {
		log.debug("获取查询地区: searchWord={}", searchWord);
		ListResult<Region> jsonResult = new ListResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			jsonResult.setResult(regionRepository.findByNameLikeOrSpellIgnoreCaseLike(searchWord, searchWord));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取查询地区异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/one/json")
	@ResponseBody
	public ObjectResult<?> ontJson(long regionId) {
		log.debug("获取地区: regionId={}", regionId);
		ObjectResult<Region> jsonResult = new ObjectResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			
			jsonResult.setResult(regionRepository.findOne(regionId));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取地区异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

}
